package com.compty.paint.web.front;

import com.compty.core.framework.context.spring.SpringContextHolder;
import com.compty.core.framework.plugin.payment.IPaymentEvent;
import com.compty.core.framework.util.DateUtil;
import com.compty.core.framework.util.StringUtil;
import com.compty.core.struts2.BaseAction;
import com.compty.paint.domain.AdvanceLogs;
import com.compty.paint.domain.CustomerInfo;
import com.compty.paint.domain.Order;
import com.compty.paint.manager.AdvanceLogsManager;
import com.compty.paint.manager.CustomerInfoManager;
import com.compty.paint.manager.OrderManager;
import com.compty.paint.service.UserServiceFactory;


/**
 * 支付挂件
 * @author compty
 */
@SuppressWarnings("unchecked")
public class PayWidget extends BaseAction {
	
	private static final long serialVersionUID = -381263837567773842L;
	
	private OrderManager orderManager;
	private AdvanceLogsManager advanceLogsManager;
	private CustomerInfoManager customerInfoManager;
	
	public String execute() {
		String action = request.getParameter("action");
		if(null== action || "".equals(action) ){
			this.pay();
		}else if("prestore".equals(action)){
			this.prestore();
		}else if("prestore_success".equals(action)){
			String amount_money = request.getParameter("amount_money");
			Double modify_advance = Double.valueOf(amount_money);
			
			String payment_name = request.getParameter("payment_name");
			
			this.editSaveAdvance(modify_advance,payment_name);
			
			return "prestore_success";
		}
		
		return SUCCESS;
	}
	
	/**
	 * 预存款充值
	 * @param modify_advance 充值金额
	 */
	public void editSaveAdvance(Double modify_advance,String payment_name) {
		CustomerInfo member  = UserServiceFactory.getUserService().getCurrentMember();
		double  advance =  member.getAdvance() == null ? 0: member.getAdvance();
		member.setAdvance( advance + modify_advance);
		try {
			customerInfoManager.save(member);
		} catch (Exception e) {
			e.printStackTrace();
		}

		AdvanceLogs advanceLogs = new AdvanceLogs();
		advanceLogs.setMember_id(member.getId());
		advanceLogs.setDisabled("false");
		advanceLogs.setMtime(DateUtil.getNow());
		advanceLogs.setImport_money(modify_advance);
		advanceLogs.setExplode_money(null);
		advanceLogs.setMember_advance(member.getAdvance());
		advanceLogs.setShop_advance(member.getAdvance());// 此字段很难理解
		advanceLogs.setMoney(modify_advance);
		
		advanceLogs.setMessage("admin代充值,充值方式:"+payment_name);
		advanceLogs.setMemo(payment_name);
		try {
			advanceLogsManager.add(advanceLogs);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String pay_confirm(){
		
		Integer orderId = this.getIntParam("orderid"); 
		
		Order order  = orderManager.get(orderId);
		double payMoney = order.getOrder_amount();
		
		request.setAttribute("orderid", orderId);
		request.setAttribute("payMoney", payMoney);
		
		return "pay_confirm";
	}

	/***
	 * 充值
	 */
	private void prestore() {
		String payment_name = request.getParameter("payment_name");
		
		String amount_money = request.getParameter("amount_money");
		if(!StringUtil.isNumber(amount_money)){
			request.setAttribute("resultMsg", "充值金额只能为数字");
			return;
		}
		Double order_amount = Double.valueOf(amount_money);
		
		String payhtml = "";
		if("alipay".equals(payment_name)){
			IPaymentEvent  paymentPlugin = SpringContextHolder.getBean("alipay");
			payhtml = paymentPlugin.onPreStore(order_amount);
		}
		else if("tenpay".equals(payment_name)){
			IPaymentEvent  paymentPlugin =  SpringContextHolder.getBean("tenpay");
			payhtml = paymentPlugin.onPreStore(order_amount);
		}else{
			
		}
		
		request.setAttribute("resultMsg", payhtml);
		
	}


	private void pay(){
		
		Integer orderId = this.getIntParam("orderid"); 
		String payment_name = request.getParameter("payment_name");
		String payment_type = request.getParameter("payment_type");
		 
		Order order  = this.orderManager.get(orderId);
		
		String payhtml = "";
		if("alipay".equals(payment_name)){
			IPaymentEvent  paymentPlugin = SpringContextHolder.getBean("alipay");
			payhtml = paymentPlugin.onPay(order);
			order.setPayment_name("支付宝");
		}
		else if("tenpay".equals(payment_name)){
			IPaymentEvent  paymentPlugin =  SpringContextHolder.getBean("tenpay");
			payhtml = paymentPlugin.onPay(order);
			order.setPayment_name("财付通");
		}else{
			
		}
		
		order.setPayment_type(payment_type);
		orderManager.save(order);

		request.setAttribute("resultMsg", payhtml);
	}

	
	private Integer getIntParam(String name){
		try{
		 return Integer.valueOf( request.getParameter(name) );
		}catch(RuntimeException e){
			e.printStackTrace();
			return null;
		}
	}

	public void setOrderManager(OrderManager orderManager) {
		this.orderManager = orderManager;
	}
	
	public void setAdvanceLogsManager(AdvanceLogsManager advanceLogsManager) {
		this.advanceLogsManager = advanceLogsManager;
	}

	public void setCustomerInfoManager(CustomerInfoManager customerInfoManager) {
		this.customerInfoManager = customerInfoManager;
	}
	
}
